package com.wd.jetpack.ui.main

import android.os.Bundle
import android.view.View
import com.wd.jetpack.R
import com.wd.jetpack.base.BaseActivity
import com.wd.jetpack.databinding.ActivityDatabingBinding
import com.wd.jetpack.ui.viewmodel.DataBindingViewModel
import com.wd.jetpack.util.Logger

/**
 * 双向绑定<BR>
 *
 * @author w4425
 * @version [V5.9.3, 2021/6/4]
 * @since V5.9.3
 *
 * 双向绑定： @={vm.XX} viewModel 中创建可关查着对象， 用来保存数据内容
 * https://developer.android.google.cn/topic/libraries/data-binding/two-way?hl=zh_cn
 *
 * 使用自定义双向绑定时注意不要引入无限循环:
 * https://developer.android.google.cn/topic/libraries/data-binding/two-way?hl=zh_cn#infinite-loops
 */
class DataBindingActivity : BaseActivity<ActivityDatabingBinding>() {
    private val mViewModel: DataBindingViewModel by lazy {
        DataBindingViewModel()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_databing
    }

    override fun initView() {
        mBinding.vm = mViewModel
    }

    override fun initData() {
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.btn_login -> {
                Logger.i("用户名：${mViewModel.userName.get()}")
                Logger.i("用户密码：${mViewModel.userPwd.get()}")
            }
        }
    }

    fun <T, R> T.myRun(m: T.() -> R): R = m()

}